/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Login;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lab1
 */
public class BOLAdimUser {

    public List<Users> showAllUser() {
        DAL dal = new DAL();
        List<Users> listUser = new ArrayList<Users>();
        try {
            ResultSet rs = dal.executeQuery("showAllUser", null);
            while (rs.next()) {
                Users us = new Users();
                us.setIdUser(rs.getString("IDUser"));
                us.setAccount(rs.getString("Account"));
                us.setPassword(rs.getString("password"));
                us.setEmail(rs.getString("Email"));
                us.setType(rs.getString("TypeUser"));
                us.setFullname(rs.getString("Fullname"));
                us.setBirthdate(rs.getDate("birthday"));
                us.setSex(rs.getString("sex"));
                us.setAge(rs.getInt("age"));
                us.setCity(rs.getString("City"));
                us.setAddress(rs.getString("Address"));
                us.setSecurity(rs.getString("Security"));
                us.setActive(rs.getString("active"));
                us.setStatus(rs.getString("status"));
                listUser.add(us);
            }
        } catch (Exception e) {
        } finally {
            dal.closeConnection();
        }
        return listUser;
    }
    public List<Users> SearchUser(String option,String value) {
        DAL dal = new DAL();
        List<Users> listUser = new ArrayList<Users>();
        String stor="";
            if(option.equals("Account"))
            {
                stor="searchUserByAccount";
            }else{
                stor="searchUserByFullname";
            }
        ArrayList ar=new ArrayList();
        ar.add(value);
        try {
            ResultSet rs = dal.executeQuery(stor,ar);
            while (rs.next()) {
                Users us = new Users();
                us.setIdUser(rs.getString("IDUser"));
                us.setAccount(rs.getString("Account"));
                us.setPassword(rs.getString("password"));
                us.setEmail(rs.getString("Email"));
                us.setType(rs.getString("TypeUser"));
                us.setFullname(rs.getString("Fullname"));
                us.setBirthdate(rs.getDate("birthday"));
                us.setSex(rs.getString("sex"));
                us.setAge(rs.getInt("age"));
                us.setCity(rs.getString("City"));
                us.setAddress(rs.getString("Address"));
                us.setSecurity(rs.getString("Security"));
                us.setActive(rs.getString("active"));
                us.setStatus(rs.getString("status"));
                listUser.add(us);
            }
        } catch (Exception e) {
            e.getMessage();
        } finally {
            dal.closeConnection();
        }
        return listUser;
    }
    public void DeleteUser(String idUser)
    {
        DAL dal = new DAL();
        ArrayList ar=new ArrayList();
        ar.add(idUser);
        try {
            try {
                int a = dal.executeUpdateQuery("deleteUser", ar);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(BOLAdimUser.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(BOLAdimUser.class.getName()).log(Level.SEVERE, null, ex);
            }
        } finally {
            dal.closeConnection();
        }
    }

    void activeUser(Users us) {
        try {
            DAL dal = new DAL();            
            String tg=us.getStatus();            
            if (tg.equalsIgnoreCase("unlocked")) tg="locked"; else tg="unlocked";
            String active=tg;
            us.setStatus(active);
            ArrayList ar=new ArrayList();
            ar.add(us.getIdUser());
            ar.add(us.getStatus());
            int a=dal.executeUpdateQuery("activeUser", ar);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(BOLAdimUser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(BOLAdimUser.class.getName()).log(Level.SEVERE, null, ex);
        }
            
            
        
    }
}
